﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class ProductType : VIT.DataHelper.DataAccess.SQLSupport
    {
        #region Entities
        public string Id { get; set; }
        public string LanguageId { get; set; }
        public string Title { get; set; }        
        #endregion

        #region Constructor
        public ProductType(string id = "",
                        string languageId = "",
                        string title = "")
        {
            this.Id = id;
            this.LanguageId = languageId;
            this.Title = title;
        }
        #endregion

        #region Methods
        public DataTable GetAll()
        {
            string strQuery = "select * from Product_Type where LanguageId=@LanguageId";
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            return ExecuteDataset(false, strQuery, LanguageId).Tables[0];
        }

        public void GetSingle()
        {
            string strQuery = "select * from Product_Type where LanguageId=@LanguageId and Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.Title = dt.Rows[0]["Title"].ToString();                
            }
            else this.Id = "";
        }

        public bool Exist(string id, string languageid)
        {
            string strQuery = "select Id from Product_Type where Id=@Id and LanguageID=@LanguageId";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", languageid);
            return ExecuteScalar(false, strQuery, Id, LanguageId) != null;
        }

        public bool Update()
        {
            string strQuery = "update Product_Type set Title=@Title where Id=@Id and LanguageId=@LanguageId";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            
            return ExecuteNonQuery(false, strQuery, Id, LanguageId, Title) > 0;
        }
        #endregion
    }
}
